package com.pan.crawler.auto.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum AutoValueTools {
    INSTANCE;
    private Pattern valuePattern = Pattern.compile("^\\s*(text|attr)\\s*:?\\s*(\\w+)?$");

    public class Value{
        boolean isAttr;
        String attrName;

        public Value(boolean isAttr, String attrName) {
            this.isAttr = isAttr;
            this.attrName = attrName;
        }

        public boolean isAttr() {return isAttr;}
        public String getAttrName() {return attrName;}
    }

    public Value parse(String valueStr) throws RuntimeException{
        Matcher matcher = valuePattern.matcher(valueStr);
        boolean matches = matcher.matches();
        boolean isAttr;
        String attrName = null;
        if (matches){
            if (matcher.group(1).equals("text")){
                isAttr = false;
            }else {
                isAttr = true;
                attrName = matcher.group(2);
            }
        }else {
            throw new RuntimeException("Error AutoValue: "+valueStr);
        }
        return new Value(isAttr, attrName);
    }
}
